from rest_framework import serializers
from django.contrib.auth import get_user_model
from django.core.validators import FileExtensionValidator

OAUser = get_user_model()

class AddStaffSerializer(serializers.Serializer):
    realname = serializers.CharField(required=True,max_length=20,error_messages={"required":"请输入员工姓名"})
    email = serializers.EmailField(error_messages={"required":"请输入员工邮箱", 'invalid':'请输入正确格式的邮箱'})
    password = serializers.CharField(max_length=20,error_messages={'required':'请输入密码'})

    def validate(self, attrs):
        request = self.context.get('request')
        email = attrs.get('email')
        if OAUser.objects.filter(email=email).exists():
            raise serializers.ValidationError("邮箱已存在")
        if request.user.department.leader.uid != request.user.uid:
            raise serializers.ValidationError("您不是部门leader,不能添加员工")
        return attrs

class ActiveStaffSerializer(serializers.Serializer):
    email = serializers.EmailField(error_messages={"required": "请输入正确的邮箱", "invalid": "请输入正确的邮箱格式"})
    password = serializers.CharField(max_length=20, error_messages={"required": "请输入密码"})

    def validate(self, attrs):
        email = attrs['email']
        password = attrs['password']
        user = OAUser.objects.filter(email=email).first()
        if not user or not user.check_password(password):
            raise serializers.ValidationError("邮箱或密码错误")
        attrs['user'] = user
        return attrs

class StaffUploadSerializer(serializers.Serializer):
    file = serializers.FileField(
        validators=[FileExtensionValidator(['xlsx', 'xls'])],
        error_messages={'required': '请上传文件'}
    )